﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Coway.Ordering.Orders
{
    public class OrderManager : DomainService, IOrderManager
    {
        private readonly IRepository<Order, Guid> _orderRepository;
        private readonly IRepository<OrderItem, Guid> _orderItemRepository;

        public OrderManager(IRepository<Order, Guid> orderRepository,
            IRepository<OrderItem, Guid> orderItemRepository)
        {
            _orderRepository = orderRepository;
            _orderItemRepository = orderItemRepository;
        }

        public async Task<Order> GetOrder(Guid orderId)
        {
            var order = await _orderRepository.GetAsync(orderId,true);
            var orderItems = await _orderItemRepository.GetListAsync();
            var items = orderItems.Where(a => a.OrderId == orderId).ToList();
            order.SetOrderItems(items ?? new List<OrderItem>());
            return order;
        }
    }
}
